<?php

namespace App\Logics;
// +----------------------------------------------------------------------
// | Copyright (c) 2024~至今 迪钰国际旅行社 版权所有.
// +----------------------------------------------------------------------
// | Author: dxf &lt;xiangfei2007@126.com&gt;
// +----------------------------------------------------------------------
// | Date: 2024/8/21 14:08
// +----------------------------------------------------------------------

use App\Exceptions\InternalException;
use App\Models\Dict;
use App\Services\DictService;

class DictLogic extends BaseLogic
{
    /**
     * 列表
     * @param array $params
     * @param int $page_size
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * Date:    2024/8/21 15:02
     */
    public function index(array $params, int $page_size): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        $query = Dict::query();
        $keyword = $params['keyword'] ?? '';
        $query->when($keyword, function ($query) use ($keyword) {
            $query->where(function ($q) use ($keyword) {
                $json_keyword = str_replace(['"', '\\'], ['', '_'], json_encode($keyword));
                $q->where('slug', 'like', '%\\' . $keyword . '%')->orWhere('name', 'like', '%\\' . $keyword . '%')->orWhere('value', 'like', '%' . $json_keyword . '%');
            });
        });
        return $query->orderByDesc('id')->paginate($page_size);
    }

    /**
     * 添加
     * @param array $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * Date:    2024/8/21 15:42
     */
    public function add(array $params): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder
    {
        $params['value'] = json_encode(array_column($params['value'], 'item', 'key'));
        (new DictService())->setSlugConfig($params['slug'],$params['value']);
        return Dict::query()->create($params);
    }

    /**
     * 删除
     * @param $id
     * @return int
     * Date:    2024/8/22 17:43
     */
    public function del($id): int
    {
        $current = Dict::findOrFail($id);
//        if(!$current){
//            throw new InternalException('数据不存在');
//        }
        (new DictService())->delSlugConfig($current['slug']);
        return $current->delete();
    }

    /**
     * 修改
     * @param array $params
     * @param int $id
     * @return true
     * @throws InternalException
     * Date:    2024/8/23 12:21
     */
    public function edit(array $params,int $id)
    {
        $current = Dict::query()->where('id',$id)->first();
        if(!$current) throw new InternalException('数据不存在');
        //排除自身的唯一性校验 不在表单验证器中做了，容易sql注入
        $check_unique = Dict::query()->where('id','<>',$id)->where('slug',$params['slug'])->exists();
        if($check_unique){
            throw new InternalException('字典标识已经存在');
        }
        $params['value'] = json_encode(array_column($params['value'], 'item', 'key'));
        $current->value = $params['value'];
        $current->name = $params['name'];
        $current->slug = $params['slug'];
        $current->save();
        (new DictService())->setSlugConfig($params['slug'],$params['value']);
        return true;
    }

}
